package com.bright.hxj.cwduijie.service.impl;

import com.bright.ghj.common.util.StringUtil;
import com.bright.hxj.cwduijie.enums.SJJHBZ;
import com.bright.hxj.cwduijie.pojo.dto.QueryTaskDto;
import com.bright.hxj.cwduijie.pojo.dto.QueryTaskReturnDto;
import com.bright.hxj.cwduijie.pojo.dto.SendDataReturnDto;
import com.bright.hxj.cwduijie.pojo.dto.SendFileDto;
import com.bright.hxj.cwduijie.service.WebExchangeService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author hxj
 * @Date 2024/7/23 14:53
 * @Description 测试类 不真正发送请求
 */

@Qualifier("testNoSend")
//@Component
@RequiredArgsConstructor
@Slf4j
public class TestNoSendWebExchangeServiceImpl implements WebExchangeService {

    private final ObjectMapper objectMapper;

    @Override
    public SendDataReturnDto sendData(Object data, String serviceCode) {
        String dataStr;
        try {
            dataStr = objectMapper.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return sendData(dataStr, serviceCode);
    }

    @Override
    public SendDataReturnDto sendData(String dataString, String serviceCode) {
        log.debug("注意现在是测试状态，并不真正发送请求");
        SendDataReturnDto sendDataDto = new SendDataReturnDto();
        sendDataDto.setIsSuccess(true);
        sendDataDto.setErrorCode(null);
        sendDataDto.setMessage("成功");
        sendDataDto.setTaskId("test-" + System.currentTimeMillis());
        return sendDataDto;
    }

    @Override
    public SendDataReturnDto sendFile(SendFileDto sendFileDto, String serviceCode) throws IOException {
        return sendData(null, serviceCode);
    }

    @Override
    public List<QueryTaskReturnDto> queryTask(QueryTaskDto queryTaskDto) {
        List<QueryTaskReturnDto> returnValue = new ArrayList<>(queryTaskDto.getTaskIds().size());
        for (String taskId : queryTaskDto.getTaskIds()) {
            if (!StringUtil.startsWith(taskId, "test")) {
                continue;
            }

            QueryTaskReturnDto dto = new QueryTaskReturnDto();
            dto.setTaskId(taskId);
            dto.setTaskState(902);
            int sjjhbz = SJJHBZ.SUCCESS.getValue();
            String message = "测试！！" + SJJHBZ.SUCCESS.getDescription();
            dto.setSjjhbz(sjjhbz);
            dto.setMessage(message);

            returnValue.add(dto);
        }
        return returnValue;
    }
}
